#include <esp_system.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

void restart_machine(void *task_params)
{
    int *param = (int *)task_params;
    int wait = *param;

    // Restart module
    while (1)
    {
        ESP_LOGI("TASK", "Restarting in %d seconds...\n", wait);
        if (wait < 0)
        {
            break;
        }
        wait = wait - 5;
        vTaskDelay(pdMS_TO_TICKS(5000));
    }
    ESP_LOGI("TASK", "Restarting now.\n");
    fflush(stdout);
    esp_restart();
}

void run_restart_task(int wait)
{
    ESP_LOGI("TASK", "Run restart task\n");
    xTaskCreatePinnedToCore(restart_machine, "restart machine", 2048, (void *)&wait, 3, NULL, 0);
}